package com.robotserver.orders.jobs.xxljob;

import com.robotserver.orders.common.DateUtil;
import com.robotserver.orders.constant.CacheKey;
import com.robotserver.orders.param.DyOrderSyncParam;
import com.robotserver.orders.service.TbOrderService;
import com.robotserver.orders.service.DyOrderService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class DyOrderSyncXxlJob {
    private static Logger logger = LoggerFactory.getLogger(DyOrderSyncXxlJob.class);

    @Autowired
    private DyOrderService dyOrderService;
    /**
     * 生产拉取抖音订单的tokens
     * 每30秒钟执行一次拉取订单（时间范围是3分钟）
     */
    @XxlJob("pushDyOrdersHander_producer")
    public void pushDyOrdersHander_producer() {
        logger.info("--------DyOrderSyncXxlJob pushDyOrdersHander_producer------start------");

        //默认持续时间为1分钟
        Long duration = 60L;
        String durationStr = "";
        try {
            //接受参数-拉取当前日期往前的几秒订单
            durationStr = XxlJobHelper.getJobParam();
            if(StringUtils.isNotBlank(durationStr)){
                //从XXL-Job接受参数
                duration = Long.parseLong(durationStr);
            }
        }catch (Exception e){
            logger.error("xxl-job para set is error {}:"+durationStr);
            //默认设置一分钟
            duration = 60L;
        }

        Integer tmptime = DateUtil.getNowTime();
        Long end_time = Long.valueOf(tmptime);                //截至时间为当前时间
        Long begin_time = tmptime - duration;             //起始时间为前2分钟
        DyOrderSyncParam dyOrderSyncParam = new DyOrderSyncParam();
        dyOrderSyncParam.setBegin_time(begin_time);
        dyOrderSyncParam.setEnd_time(end_time);
        dyOrderSyncParam.setRedisQueueKey(CacheKey.dyPayOrderQueue);
        dyOrderSyncParam.setDownTimeType(4);     //更新时间
        //调用生产者线程，遍历抖音联盟，往redis队列中添加dyTokens
        dyOrderService.syncOrder_producer(dyOrderSyncParam);
        logger.info("-------------往redis中推入抖音联盟------dyOrderService=" + dyOrderService.toString());
    }

    /**
     * 消费抖音tokens拉取各种订单信息
     * 每30秒钟执行一次拉取订单（时间范围是3分钟）
     */
    @XxlJob("pushDyOrdersHander_consumer")
    public void pushDyOrdersHander_consumer() {
        logger.info("--------DyOrderSyncXxlJob pushDyOrdersHander_consumer------start------");
        //调用消费者线程，遍历redis队列，消费dyTokens
        DyOrderSyncParam dyOrderSyncParam = new DyOrderSyncParam();
        dyOrderSyncParam.setRedisQueueKey(CacheKey.dyPayOrderQueue);
        dyOrderSyncParam.setDownTimeType(4);     //更新时间
        dyOrderService.syncOrder_consumer(dyOrderSyncParam);
        logger.info("---------------------------消费抖音联盟-----------------dyOrderSyncParam=" + dyOrderSyncParam.toString());
    }

}
